package com.mmall.service.impl;

import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import com.mmall.common.ServerResponse;
import com.mmall.dao.CategoryMapper;
import com.mmall.pojo.Category;
import com.mmall.service.ICategoryService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Set;

/**
 * Created by Administrator on 2018/11/9.
 */
@Service("iCategoryService")
public class CategoryServiceImpl implements ICategoryService {

    private Logger logger = LoggerFactory.getLogger(CategoryServiceImpl.class);

    @Autowired
    private CategoryMapper categoryMapper;

    /**
     * 添加品类
     *
     * @param categoryName
     * @param parentId
     * @return
     */
    public ServerResponse addCategory(String categoryName, Integer parentId) {
//        校验参数
        if (parentId == null || StringUtils.isBlank(categoryName)) {
            return ServerResponse.createByErrorMessage("添加品类错误");
        }
//        校验通过
        Category category = new Category();
        category.setName(categoryName);
        category.setParentId(parentId);
//        设置状态，为这个分类可用
        category.setStatus(true);
//        插入数据库
        int rowCount = categoryMapper.insert(category);
        if (rowCount > 0) {
            return ServerResponse.createBySuccess("添加品类成功");
        }
        return ServerResponse.createByErrorMessage("添加品类失败");
    }

    /**
     * 更新品类
     *
     * @param categoryId
     * @param categoryName
     * @return
     */
    public ServerResponse updateCategoryName(Integer categoryId, String categoryName) {
//        对参数进行校验
        if (categoryId == null || StringUtils.isBlank(categoryName)) {
            return ServerResponse.createByErrorMessage("更新参数错误");
        }
//        验证通过后，进行赋值
        Category category = new Category();
        category.setName(categoryName);
        category.setId(categoryId);

//        选择有选择性更新的那个方法
        int resultCount = categoryMapper.updateByPrimaryKeySelective(category);
        if (resultCount > 0) {
            return ServerResponse.createBySuccessMessage("更新品类名字成功");
        }
        return ServerResponse.createByErrorMessage("更新品类名字失败");
    }

    public ServerResponse<List<Category>> getChildrenParallelCategory(Integer categoryId) {

//        通过品类id查出子节点的平行节点，并作判断
        List<Category> categoryList = categoryMapper.selectCategoryChildrenByParentId(categoryId);
        if (CollectionUtils.isEmpty(categoryList)) {
            logger.info("未找到当前节点的子分类");
        }
        return ServerResponse.createBySuccess(categoryList);
    }

    /**
     * 递归查询本节点的id以及子节点的id
     *
     * @param categoryId
     * @return
     */
    public ServerResponse<List<Integer>> selectCategoryAndChildrenById(Integer categoryId) {
//        用guava里面的方法初始化
        Set<Category> categorySet = Sets.newHashSet();
//        调用递归方法
        findChildCategory(categorySet, categoryId);

//        需要获取所有节点的id，这里创建一个列表容器用来放这些
        List<Integer> categoryIdList = Lists.newArrayList();

        if (categoryId != null) {
//            遍历categorySet
            for (Category categoryItem : categorySet) {
//                获取每个查询到的category的id
                categoryIdList.add(categoryItem.getId());
            }
        }
//        返回categoryIdList
        return ServerResponse.createBySuccess(categoryIdList);
    }

    //    递归算法，算出子节点
    private Set<Category> findChildCategory(Set<Category> categorySet, Integer categoryId) {
//        根据categoryId查询这个节点的信息
        Category category = categoryMapper.selectByPrimaryKey(categoryId);
        if (category != null) {
//            将查询到的Category信息放入这个Set数据类型中
            categorySet.add(category);
        }
//        查找子节点，递归算法有一个退出的条件。根据categoryId查询他这个节点下的子节点
        List<Category> categoryList = categoryMapper.selectCategoryChildrenByParentId(categoryId);
//        查询子节点的子节点
        for (Category categoryItem : categoryList) {
//            每遍历一个子节点调用下方法自己，再进行遍历下
            findChildCategory(categorySet, categoryItem.getId());
        }
//        最后返回查询到的结果
        return categorySet;
    }


}
